/*!
    \copyright  Copyright (c) 2019 - 2025 Qualcomm Technologies International, Ltd.
                All Rights Reserved.
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \file
    \ingroup    ui_indicator
    \brief      Functionality to allow the Application to register its own plug-in for 
                mamanging which audio prompts to play. E.g. as an ADK add-on.
*/

#include "ui_indicator_log_level.h"
#include "ui_indicator_prompts.h"
#include "ui_indicator_private.h"
#include "ui_indicator.h"
#include "ui.h"
#include "ui_inputs.h"

#include <logging.h>
#include <panic.h>

extern ui_prompts_task_data_t the_prompts;

#define PROMPT_NONE                     0xFFFF

#define USER_FUNC_TABLE_GROW_SHRINK_THRESH    5

static bool uiPrompts_IsUserHandledPromptIndex(MessageId id, size_t *index)
{
    bool result = FALSE;
    for (size_t i = 0 ; i < the_prompts.num_user_prompts ; i++)
    {
        if (the_prompts.user_prompts[i].message_id == id)
        {
            *index = i;
            result = TRUE;
            break;
        }
    }
    return result;
}

bool uiPrompts_IsUserHandledPrompt(MessageId id)
{
    bool result;
    size_t dummy;
    result = uiPrompts_IsUserHandledPromptIndex(id,&dummy);
    UNUSED(dummy);
    return result;
}

const ui_event_indicator_table_t *uiPrompts_GetDataForUserEvent(MessageId id)
{
    const ui_event_indicator_table_t *result = NULL;
    size_t index = 0;
    if (uiPrompts_IsUserHandledPromptIndex(id, &index))
    {
        PanicFalse(the_prompts.user_prompts[index].user_fn != 0);
        result = the_prompts.user_prompts[index].user_fn(id);
    }
    PanicFalse(result != NULL);
    return result;
}

const ui_prompt_data_t *uiPrompts_GetDataForUserPrompt(MessageId id)
{
    const ui_prompt_data_t *result = NULL;
    const ui_event_indicator_table_t *table = uiPrompts_GetDataForUserEvent(id);
    result = &table->data.prompt;
    return result;
}

bool uiPrompts_IsIdInUserTable(MessageId id)
{
    for (unsigned i = 0 ; i < the_prompts.num_user_prompts; i++)
    {
        if (the_prompts.user_prompts[i].message_id == id)
        {
            return TRUE;
        }
    }
    return FALSE;
}

void UiPrompts_SetUserPromptDataFunction(ui_prompts_data_fn fn, MessageId id)
{
    PanicNull((void *)fn);

    if (!uiPrompts_IsIdInUserTable(id))
    {
        if (the_prompts.num_user_prompts == the_prompts.user_prompts_len)
        {
            size_t new_size = sizeof(ui_prompts_user_config_t) * (the_prompts.num_user_prompts + USER_FUNC_TABLE_GROW_SHRINK_THRESH);
            the_prompts.user_prompts = PanicNull(realloc(the_prompts.user_prompts, new_size));
            the_prompts.user_prompts_len += USER_FUNC_TABLE_GROW_SHRINK_THRESH;
            for (unsigned i = the_prompts.num_user_prompts ; i < the_prompts.user_prompts_len; i++)
            {
                const ui_prompts_user_config_t empty = { 0 };
                the_prompts.user_prompts[i] = empty;
            }
        }
        // Appends new event,handler tuple at end of array
        unsigned i = the_prompts.num_user_prompts;
        PanicFalse(the_prompts.user_prompts[i].user_fn == NULL);
        the_prompts.user_prompts[i].user_fn = fn;
        the_prompts.user_prompts[i].message_id = id;
        the_prompts.num_user_prompts++;
        UiIndicator_RegisterInterestInSystemEventGroup(id, &the_prompts.task);
    }

    DEBUG_LOG_VERBOSE("UiPrompts_SetUserPromptDataFunction id=0x%04x, num=%d, length=%d", id, the_prompts.num_user_prompts, the_prompts.user_prompts_len);
}

void UiPrompts_ClearUserPromptDataFunction(MessageId id)
{
    bool cleared = FALSE;
    unsigned i = 0;
    for (i = 0; i < the_prompts.user_prompts_len; i++)
    {
        if (the_prompts.user_prompts[i].message_id == id)
        {
            the_prompts.num_user_prompts--;
            cleared = TRUE;
            break;
        }
    }
    if (cleared)
    {
        for (; i < (the_prompts.user_prompts_len-1); i++)
        {
            the_prompts.user_prompts[i] = the_prompts.user_prompts[i+1];
        }
        const ui_prompts_user_config_t empty = { 0 };
        the_prompts.user_prompts[i] = empty;
    }
    if ((the_prompts.user_prompts_len - USER_FUNC_TABLE_GROW_SHRINK_THRESH) > the_prompts.num_user_prompts)
    {
        size_t new_size = sizeof(ui_prompts_user_config_t) * the_prompts.num_user_prompts;
        the_prompts.user_prompts = PanicNull(realloc(the_prompts.user_prompts, new_size));
        the_prompts.user_prompts_len = the_prompts.num_user_prompts;
    }

    DEBUG_LOG_VERBOSE("UiPrompts_ClearUserPromptDataFunction id=0x%04x, num=%d, len=%d", id, the_prompts.num_user_prompts, the_prompts.user_prompts_len);
}
